package com.hfb.mashibing.alip8.system.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Objects;

public class T00_BIO_TestSocket {

    public static void main(String[] args) throws IOException {
        ServerSocket socket = new ServerSocket(9000);
        System.out.println("step1: new ServerSocket(9090)");

        while (true) {
            Socket client = socket.accept(); // 阻塞1
            System.out.println("step2: client \t" + client.getPort());

            new Thread(()->{
                try(InputStream in = client.getInputStream();
                    BufferedReader reader = new BufferedReader((new InputStreamReader(in)));){

                    while (true) {
                        String dataLine = reader.readLine();
                        if (Objects.nonNull(dataLine)) {
                            System.out.println(dataLine); // 阻塞2
                        } else {
                            client.close();
                            break;
                        }
                    }
                    System.out.println("客户端断开");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();

        }

    }

}
